package service;

import dao.RecordDAO;
import entity.Record;
import util.DateUtil;

import java.time.LocalDate;
import java.util.*;

public class ReportService {
    public int getTodaySpend(Date d, List<Record> monthRawData) {
        int daySpend = 0;

        //消除日期格式不同，LocalDateJAVA8新特性
        Date date = DateUtil.util2sql(d);
        LocalDate d_ld = LocalDate.parse(date.toString());

        for(Record record : monthRawData) {
            LocalDate record_ld = LocalDate.parse(record.date.toString());

            if(d_ld.equals(record_ld)) {
                daySpend += record.spend;
                //System.out.println("record.date.equals(d)");
            }
            //System.out.println(d_ld+"__"+record_ld);
        }
        return daySpend;
    }

    public List<Record> listThisMonthRecords() {
        RecordDAO recordDAO = new RecordDAO();
        List<Record> monthRawData = recordDAO.listThisMonth();
        List<Record> result = new ArrayList<>();
        Date monthBegin = DateUtil.monthBegin();
        int monthTotalDay = DateUtil.thisMonthTotalDay();
        Calendar c = Calendar.getInstance();
        //System.out.println(monthTotalDay);
        for(int i = 0; i < monthTotalDay; i++) {
            Record r = new Record();
            c.setTime(monthBegin);
            c.add(Calendar.DATE,i);
            Date eachDayOfThisMonth = c.getTime();
            int daySpend = getTodaySpend(eachDayOfThisMonth,monthRawData);
            r.setSpend(daySpend);
            result.add(r);
        }
        return result;
    }
}
